/**
 * @file default_task.c
 * @author jiaqi.lou  (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-12-07
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include "default_task.h"

/* Definitions for defaultTask */
TaskHandle_t defaultTaskHandle;

/* Definition for static function*******************************************/
static led_status_t led_geen_on(void);

static led_status_t led_geen_off(void);

static void StartDefaultTask(void *argument);

led_status_t (default_get_tick)(uint32_t * const p_tick);

led_status_t (default_os_delay)(const uint32_t delay);


static led_operation_t led_operation = {
    .pf_led_on = led_geen_on,
    .pf_led_off = led_geen_off,
};

static led_timebase_t led_timebase = {
    .pf_led_get_tick = default_get_tick,
};

static led_os_delay_t led_os_delay = {
    .pf_led_os_delay = default_os_delay,
};  

/**********************Private Function************************************************ */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
static void StartDefaultTask(void *argument)
{
    bsp_led_driver_t xled_green_inst;
    bsp_led_instance(&xled_green_inst, &led_operation, &led_timebase, &led_os_delay);
    xled_green_inst.pf_led_control(&xled_green_inst, 1000, 4, PROPORTION_1_1);

    for(;;)
    {
        printf("Hello World!\r\n");
        vTaskDelay(1000);
    }

}
/**
 * @brief turn on led green
 * 
 * @return led_status_t 
 */
static led_status_t led_geen_on(void)
{
    LedTask(LED_GREEN);
    return LED_OK;
}

/**
 * @brief turn off led green    
 * 
 * @return led_status_t 
 */
static led_status_t led_geen_off(void)
{
    LedTask(LED_NONE);
    return LED_OK;
}

/**
 * @brief get os system tick
 * @param[out] p_tick: system tick
 */
led_status_t (default_get_tick)(uint32_t * const p_tick){
     TickType_t currentTick = xTaskGetTickCount();

    // 将tick转换为毫秒
    uint32_t currentTimeMs = currentTick * portTICK_PERIOD_MS;
    *p_tick = currentTimeMs;
}

/**
 * @brief os delay
 * @param[in] delay: delay time
 */
led_status_t (default_os_delay)(const uint32_t delay){
    vTaskDelay(pdMS_TO_TICKS(delay));
    return LED_OK;
}


/* *************************Public Interface**************************************/
bool DefaultTaskInit(void)
{
    BaseType_t val;
    val = xTaskCreate(StartDefaultTask, "defaultTask", 256, NULL, 1, &defaultTaskHandle);
    if(val != pdPASS)
    {
        printf("defaultTask create failed!\r\n");
        return false;
    }
    return true;
  
}   
